package com.engine4cloud.tdf.supervision.service.impl;

import com.engine4cloud.tdf.admin.api.feign.RemoteUserService;
import com.engine4cloud.tdf.common.core.constant.SecurityConstants;
import com.engine4cloud.tdf.common.core.constant.enums.AllAuditActionStatusEnum;
import com.engine4cloud.tdf.common.core.constant.enums.AuditBusinessTypeEnum;
import com.engine4cloud.tdf.common.core.util.R;
import com.engine4cloud.tdf.common.core.util.RetOps;
import com.engine4cloud.tdf.common.security.util.SecurityUtils;
import com.engine4cloud.tdf.common.sse.config.SseEmitterMessageSender;
import com.engine4cloud.tdf.supervision.service.SpAuditMessageService;
import jakarta.annotation.Resource;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class SpAuditMessageServiceImpl implements SpAuditMessageService {

    @Resource
    private RemoteUserService remoteUserService;

    @Override
    public void sendMessageToParent(AuditBusinessTypeEnum businessType, AllAuditActionStatusEnum action) {
        R<List<Long>> parentDeptUserIdList = remoteUserService.getParentUserIdListByDeptId(SecurityUtils.getUser().getDeptId(), SecurityConstants.FROM_IN);
        List<Long> userIdList = RetOps.of(parentDeptUserIdList).getData().orElse(null);
        if (CollectionUtils.isNotEmpty(userIdList)) {
            userIdList.forEach(userId -> SseEmitterMessageSender.send(userId, String.format("<p>您有新的%s%s信息待处理</p>", businessType.getDesc(), action.getDesc())));
        }
    }

    @Override
    public void sendMessageToLateral(AuditBusinessTypeEnum businessType, AllAuditActionStatusEnum action) {
        R<List<Long>> lateralDeptUserIdList = remoteUserService.getLateralUserIdListByDeptId(SecurityUtils.getUser().getDeptId(), SecurityConstants.FROM_IN);
        List<Long> userIdList = RetOps.of(lateralDeptUserIdList).getData().orElse(null);
        if (CollectionUtils.isNotEmpty(userIdList)) {
            userIdList.forEach(userId -> SseEmitterMessageSender.send(userId, String.format("<p>您有新的%s%s信息待处理</p>", businessType.getDesc(), action.getDesc())));
        }
    }
}
